package com.oscarmovie.action;

import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.oscarmovie.Constants;
import com.oscarmovie.model.Language;
import com.oscarmovie.model.Paging;
import com.oscarmovie.model.Video;
import com.oscarmovie.service.LanguageService;
import com.oscarmovie.service.VideoService;

public class VideoAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private VideoService videoService;
	private LanguageService languageService;
	private Video video;
	private List<Video> videos;
	private List<Language> languages;
	private Paging paging = new Paging();

	public String execute() throws Exception {
		throw new Exception("Please specify method name!");
	}
	
	public String add() throws Exception {
		video.getMovie().setNameCn(new String(video.getMovie().getNameCn().getBytes("ISO8859-1"),"UTF-8"));
		this.languages = languageService.loadAll();
		return Constants.ADD;
	}
	public String edit() throws Exception {
		list();
		load();
		return Constants.EDIT;
	}
	
	public String addVideo() throws Exception {
		video.setImage(null);
		video.setDatetime(new Date());
		videoService.save(video);
		
		return Constants.REFRESH;
	}
	
	public String deleteVideo() throws Exception {

		videoService.delete(video);
		
		return Constants.REFRESH;
	}
	
	public String save() throws Exception {
		if(video.getImage()==null || video.getImage().getId() == 0){
			video.setImage(null);
		}
		videoService.save(video);
		
		return Constants.CLOSE;
	}
	public String load() throws Exception {
		this.setVideo(videoService.load(video.getId()));
		return SUCCESS;
	}
	public String list() throws Exception {
		this.videos = videoService.list(paging);
		this.languages = languageService.loadAll();
		return SUCCESS;
	}

	public String select() throws Exception {
		list();
		return Constants.SELECT;
	}
	public String detail() throws Exception {
		load();
		return Constants.DETAIL;
	}
	public List<Video> getVideos() {
		return videos;
	}

	public void setVideoService(VideoService videoService) {
		this.videoService = videoService;
	}

	public void setVideo(Video video) {
		this.video = video;
	}

	public Video getVideo() {
		return video;
	}

	public void setLanguageService(LanguageService languageService) {
		this.languageService = languageService;
	}

	public LanguageService getLanguageService() {
		return languageService;
	}

	public void setLanguages(List<Language> languages) {
		this.languages = languages;
	}

	public List<Language> getLanguages() {
		return languages;
	}


	public void setPaging(Paging paging) {
		this.paging = paging;
	}


	public Paging getPaging() {
		return paging;
	}
}
